#ifndef THINK_THINK_ERROR_H
#define THINK_THINK_ERROR_H

static char* think_error =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think;\n"
	"\n"
	"use think\\console\\Output as ConsoleOutput;\n"
	"use think\\exception\\ErrorException;\n"
	"use think\\exception\\Handle;\n"
	"use think\\exception\\ThrowableError;\n"
	"\n"
	"class Error\n"
	"{\n"
	"    /**\n"
	"     * 注册异常处理\n"
	"     * @access public\n"
	"     * @return void\n"
	"     */\n"
	"    public static function register()\n"
	"    {\n"
	"        error_reporting(E_ALL);\n"
	"        set_error_handler([__CLASS__, \'appError\']);\n"
	"        set_exception_handler([__CLASS__, \'appException\']);\n"
	"        register_shutdown_function([__CLASS__, \'appShutdown\']);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 异常处理\n"
	"     * @access public\n"
	"     * @param  \\Exception|\\Throwable $e 异常\n"
	"     * @return void\n"
	"     */\n"
	"    public static function appException($e)\n"
	"    {\n"
	"        if (!$e instanceof \\Exception) {\n"
	"            $e = new ThrowableError($e);\n"
	"        }\n"
	"\n"
	"        $handler = self::getExceptionHandler();\n"
	"        $handler->report($e);\n"
	"\n"
	"        if (IS_CLI) {\n"
	"            $handler->renderForConsole(new ConsoleOutput, $e);\n"
	"        } else {\n"
	"            $handler->render($e)->send();\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 错误处理\n"
	"     * @access public\n"
	"     * @param  integer $errno      错误编号\n"
	"     * @param  integer $errstr     详细错误信息\n"
	"     * @param  string  $errfile    出错的文件\n"
	"     * @param  integer $errline    出错行号\n"
	"     * @return void\n"
	"     * @throws ErrorException\n"
	"     */\n"
	"    public static function appError($errno, $errstr, $errfile = \'\', $errline = 0)\n"
	"    {\n"
	"        $exception = new ErrorException($errno, $errstr, $errfile, $errline);\n"
	"\n"
	"        // 符合异常处理的则将错误信息托管至 think\\exception\\ErrorException\n"
	"        if (error_reporting() & $errno) {\n"
	"            throw $exception;\n"
	"        }\n"
	"\n"
	"        self::getExceptionHandler()->report($exception);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 异常中止处理\n"
	"     * @access public\n"
	"     * @return void\n"
	"     */\n"
	"    public static function appShutdown()\n"
	"    {\n"
	"        // 将错误信息托管至 think\\ErrorException\n"
	"        if (!is_null($error = error_get_last()) && self::isFatal($error[\'type\'])) {\n"
	"            self::appException(new ErrorException(\n"
	"                $error[\'type\'], $error[\'message\'], $error[\'file\'], $error[\'line\']\n"
	"            ));\n"
	"        }\n"
	"\n"
	"        // 写入日志\n"
	"        Log::save();\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 确定错误类型是否致命\n"
	"     * @access protected\n"
	"     * @param  int $type 错误类型\n"
	"     * @return bool\n"
	"     */\n"
	"    protected static function isFatal($type)\n"
	"    {\n"
	"        return in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE]);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 获取异常处理的实例\n"
	"     * @access public\n"
	"     * @return Handle\n"
	"     */\n"
	"    public static function getExceptionHandler()\n"
	"    {\n"
	"        static $handle;\n"
	"\n"
	"        if (!$handle) {\n"
	"            // 异常处理 handle\n"
	"            $class = Config::get(\'exception_handle\');\n"
	"\n"
	"            if ($class && is_string($class) && class_exists($class) &&\n"
	"                is_subclass_of($class, \"\\\\think\\\\exception\\\\Handle\")\n"
	"            ) {\n"
	"                $handle = new $class;\n"
	"            } else {\n"
	"                $handle = new Handle;\n"
	"\n"
	"                if ($class instanceof \\Closure) {\n"
	"                    $handle->setRender($class);\n"
	"                }\n"
	"\n"
	"            }\n"
	"        }\n"
	"\n"
	"        return $handle;\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif